<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Article;
use App\Http\Model\Category;
use App\Http\Model\Link;
use App\Http\Model\Nav;

class IndexController extends CommonController
{
    public function index()
    {
        //点击量最高的6篇文章(站长推荐)
        $phot = Article::orderBy('art_view','desc')->take(6)->get();

        //图文列表,带分页效果
        $data = Article::orderBy('art_time','desc')->paginate(5);

        //友情链接
        $links = Link::orderBy('link_order','asc')->get();
        //网站配置项

        return view('home/index',compact('phot','hot','data','new','links'));
    }

    public function cate($cate_id)
    {
        $field = Category::find($cate_id);
        //查看次数自增
        Category::where('cate_id',$cate_id)->increment('cate_view');
        // 图文列表(4篇)带分页效果
        $data = Article::where('cate_id',$cate_id)->orderBy('art_time','desc')->paginate(4);
        //当前分类的子分类
        $submenu = Category::where('cate_pid',$cate_id)->get();
        return view('home/list',compact('field','data','submenu'));
    }

    public function article($art_id)
    {
        $field = Article::Join('category','article.cate_id','=','category.cate_id')->where('art_id',$art_id)->first();
        //查看次数自增
        Article::where('art_id',$art_id)->increment('art_view');
        //上一篇
        $article['pre'] = Article::where('art_id','<',$art_id)->orderBy('art_id','desc')->first();
        //下一篇
        $article['next'] = Article::where('art_id','>',$art_id)->orderBy('art_id','asc')->first();
        //本类文章(6)篇
        $data = Article::where('cate_id',$field->cate_id)->orderBy('art_id','desc')->take(6)->get();

        return view('home.new',compact('field','article','data'));
    }
}
